package net.sxnic.ugr.dept.action;

import net.sxnic.comm.log.Log;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.role.Role;

@SuppressWarnings("serial")
public class Delete extends DeptAction {
	
	 

	public String execute() throws Exception {

		try {
			checkRoles(Role.SYSTEM_ADMIN);
			// 删除关联的用户
			dept = deptManager.get(id);

			if (dept.getChild() != null && dept.getChild().size() > 0) {
				msg = "请先删除当前部门的子部门！";
				return "right";
			}

			dept.getUsers().clear();

			deptManager.update(dept);
			
			parent = dept.getParent();
			
			if(parent != null){
				parent.getChild().remove(dept);

				deptManager.update(parent);
			}
			
			// 删除实体
			deptManager.deleteById(id);
			msg = "删除成功";

			// 日志
			addLog(Log.LOG_OPERATION_DLETE, AppContext.getUserName(request)
					+ " delete dept sucess! dept id:" + id);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "right";
	}

}
